package com.lwp.springboot.sbmybatis.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

//@Configuration(proxyBeanMethods = false)
@Configuration
public class DataSourceConfig {

    /**
     * app.datasource.url=jdbc:h2:mem:mydb
     * app.datasource.username=sa
     * app.datasource.pool-size=30
     * @return
     */
//    @Bean
//    @ConfigurationProperties(prefix = "app.datasource")
//    public SomeDataSource dataSource() {
//        return new SomeDataSource();
//    }

    /**
     * app.datasource.url=jdbc:mysql://localhost/test
     * app.datasource.username=dbuser
     * app.datasource.password=dbpass
     * app.datasource.pool-size=30
     *
     * 没有指定类型
     *      * 注意 jdbc-url
     *      *
     *      * app.datasource.jdbc-url=jdbc:mysql://localhost/test
     *      * app.datasource.username=dbuser
     *      * app.datasource.password=dbpass
     *      * app.datasource.pool-size=30
     *
     * @return
     */
    @Bean
    @ConfigurationProperties("app.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    @ConfigurationProperties("app.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * @param properties
     * @return
     */
    @Bean
    @ConfigurationProperties("app.datasource.configuration")
    public HikariDataSource dataSource(DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

}
